package com.lj.infoisland.service;

import com.lj.infoisland.domain.common.CommonOption;
import com.lj.infoisland.domain.vo.BasicsDataVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 龙洁
 * @description 数据统计Service
 * @create 2025-01-20 13:40
 */
@Service
public class DataStatService {
    @Resource
    private NewsService newsService;
    @Resource
    private NewsCommentsService newsCommentsService;
    @Resource
    private UsersService usersService;
    @Resource
    private NewsCategoriesService newsCategoriesService;

    /**
     * 获取基础数据统计：总用户、今日新增、新闻总数、评论总数
     *
     * @return
     */
    public BasicsDataVO getBasicsDataStat() {
        BasicsDataVO basicsDataVO = new BasicsDataVO();
        basicsDataVO.setTotalUsers(String.valueOf(usersService.count()));
        basicsDataVO.setNewUsers(String.valueOf(usersService.getNewUsersCount()));
        basicsDataVO.setTotalNews(String.valueOf(newsService.count()));
        basicsDataVO.setTotalComments(String.valueOf(newsCommentsService.count()));
        return basicsDataVO;
    }

    /**
     * 获取用户增长趋势
     *
     * @return
     */
    public List<Integer> getUserGrowthTrend() {
        List<Integer> userGrowthTrend = new ArrayList<>();
        // 获取七天每天的用户新增数量
        for (int i = 0; i < 7; i++) {
            userGrowthTrend.add(usersService.getWeekUserCount(i));
        }
        return userGrowthTrend;
    }

    /**
     * 获取新闻类别比例
     *
     * @return
     */
    public List<CommonOption> getNewsCategoryProportion() {
        List<CommonOption> newsCategoryProportionList = new ArrayList<>();
        newsCategoriesService.getNewsCategoriesList().getData().forEach(item -> {
            newsCategoryProportionList.add(new CommonOption(newsService.getNewsCategoryCount(item.getValue()), item.getLabel()));
        });
        return newsCategoryProportionList;
    }

    /**
     * 获取评论增长趋势
     *
     * @return
     */
    public List<Integer> getCommentTrend() {
        List<Integer> commentTrendList = new ArrayList<>();
        // 获取七天每天的用户新增数量
        for (int i = 0; i < 7; i++) {
            commentTrendList.add(newsCommentsService.getWeekNewsCommentsCount(i));
        }
        return commentTrendList;
    }
}
